<?php

namespace app\admin\controller;

use app\comm\func\CommFunc;
use app\comm\func\Consts;
use think\Controller;
use think\Request;
use think\Db;

/**
 * Class Teacher 教师模块
 * @package app\admin\controller
 */
class Teacher extends Controller
{
    /**
     * @param Request $request 教师列表
     * @return mixed
     */
    public function TeacherList(Request $request)
    {
        $resAll = Db::table('admin a')
            ->where('role_id', 15)
            ->where('is_type',1)
            ->select();
        $res = Db::query('SELECT 
                              teacher_id,t.classroom_id,t.id FROM
                              ( SELECT  max( stage ) stage,classroom_id,id  FROM teacher_class GROUP BY classroom_id ) AS t,teacher_class AS s 
                              WHERE t.stage = s.stage AND t.classroom_id = s.classroom_id');
        foreach ($res as $key => $vo) {
            $result[$key]['class'] = Db::table('classroom')->where('cid', $vo['classroom_id'])->find();
            $result[$key]['teacher'] = Db::table('admin')->where('aid', $vo['teacher_id'])->find();
        }
        $this->assign(['auth' => $resAll]);
        return $this->fetch();
    }

    /**
     * @param Request $request 教师增加
     * @return mixed
     */
    public function TeacherAdd(Request $request)
    {
        if ($request->isPost()) {
            CommFunc::emptyVerify($request->post(), Consts::$teacherAdd);
            CommFunc::conditionAdd($condition, $request->post(), Consts::$teacherAdd);
            if ($condition['password'] != $condition['password2']) {
                CommFunc::ajaxReturn(Consts::$failure);
            }
            $res = Db::table('admin')->where('name', $condition['name'])->count();
            if ($res > 0) {
                CommFunc::ajaxReturn(Consts::$failure);
            }
            $condition['name'] = trim($condition['name']);
            $condition['password'] = trim(md5($condition['password']));
            $condition['phone'] = trim($condition['phone']);
            $condition['role_id'] = Consts::$teacher_role_id;
            $condition['is_type'] = Consts::$is_type;
            unset($condition['password2']);
            $insertType = DB::table('admin')->insert($condition);
            if ($insertType > 0) {
                CommFunc::ajaxReturn(Consts::$successful);
            } else {
                CommFunc::ajaxReturn(Consts::$failure);
            }
        } else {
            $res = Db::table('classroom')->where('status', 1)->select();
            $this->assign('classroom', $res);
            return $this->fetch();
        }
    }

    /**
     * @param Request $request 教师修改
     * @return mixed
     */
    public function TeacherEdit(Request $request)
    {
        if ($request->isPost()) {
            CommFunc::emptyVerify($request->post(), Consts::$teacherEdit);
            CommFunc::conditionAdd($condition, $request->post(), Consts::$teacherEdit);
            if ($condition['password'] != $condition['password2']) {
                CommFunc::ajaxReturn(Consts::$failure);
            }
            $res = Db::table('admin')
                ->where('name', $condition['name'])
                ->where('aid', '<>', $condition['aid'])->count();
            if ($res > 0) {
                CommFunc::ajaxReturn(Consts::$failure);
            }
            $condition['name'] = trim($condition['name']);
            $condition['password'] = trim(md5($condition['password']));
            $condition['phone'] = trim($condition['phone']);
            unset($condition['password2']);
            $updateType = Db::table('admin')->where('aid', $condition['aid'])
                ->data([
                    'name' => $condition['name'],
                    'password' => $condition['password'],
                    'phone' => $condition['phone'],
                ])->update();
            if ($updateType) {
                CommFunc::ajaxReturn(Consts::$successful);
            } else {
                CommFunc::ajaxReturn(Consts::$failure);
            }
        } else {
            CommFunc::emptyVerify($request->param(), ['aid']);
            CommFunc::conditionAdd($condition, $request->param(), ['aid']);
            $res['auth'] = Db::table('admin')->where('aid', $condition['aid'])->find();
            $this->assign('data', $res);
            return $this->fetch();
        }
    }

    /**
     * @param Request $request 教师删除
     */
    public function TeacherDel(Request $request)
    {
        CommFunc::emptyVerify($request->post(), ['aid']);
        CommFunc::conditionAdd($condition, $request->post(), ['aid']);
        $res = Db::table('admin')->where('role_id', '<>', 1)
            ->where('aid', $condition['aid'])
            ->delete();
        if ($res) {
            CommFunc::ajaxReturn(Consts::$successful);
        } else {
            CommFunc::ajaxReturn(Consts::$failure);
        }
    }
}